local UIManager =BaseClass(Singleton)

local layerParents = {}        -- 各层级父节点 
 local activePanels = {}        -- 激活的面板
 local openUIList={}
function  UIManager:__init()

end

-- 初始化管理器
function UIManager:Init()
    local UIRoot = GameObject.FindWithTag("UIRoot").transform
    for k,v in pairs(UILayer) do
        
        local uiLayer = GameObject(k)
        uiLayer.name = k
        uiLayer.layer = 5;
       
       
        local canvas= uiLayer:AddComponent(typeof(Canvas))
        --canvas.overrideSorting = true
        canvas.sortingOrder = v
        uiLayer:AddComponent(typeof(GraphicRaycaster))
        uiLayer.transform:SetParent(UIRoot,false)
         local rectTransform = uiLayer:GetComponent(typeof(RectTransform))
         rectTransform.offsetMax = Vector2.one
        rectTransform.offsetMin = Vector2.zero
          rectTransform:SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,Screen.width);
        rectTransform:SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,Screen.height);
        rectTransform.localScale = Vector3.one
        rectTransform.localPosition = Vector3.zero
        layerParents[v] = uiLayer.transform
    end
end

-- 打开UI界面
function UIManager:OpenUI(uiKey)
     local config = UIConfig[uiKey]
    if not config then return end
    

    -- 互斥检查
    self:CheckMutex(config)
    local curUI = openUIList[config.uiKey] 
    if curUI~= nil then
        curUI.src:OnShow()
    else
        local obj = CS.UtilsCSharp.LoadAssetData_Game(config.uiKey)

            local uiObj = GameObject.Instantiate(obj)
            uiObj:SetActive(false)
            uiObj.name = config.uiKey
            local uiScriptsPath = "UI/UILogic/"..config.luaSrc
            local luaScript = require(uiScriptsPath).New(uiObj)
            self:SetupUI(uiObj, config)

            -- 处理模态
            if config.isModal then 
            -- self:CreateModalMask(config.layer)
            -- table.insert(modalStack, uiObj)
            end
            luaScript:OnShow()
            local ui ={  obj = uiObj,src = luaScript}
            -- 记录激活的UI
            if not activePanels[config.layer] then
            activePanels[config.layer] = {}
            end
            table.insert(activePanels[config.layer], config.uiKey)

            openUIList[config.uiKey]= ui
            -- 播放动画
            if config.useBounceEffect then 
               self:PlayBounceAnimation(uiObj)
            end
    end
end

-- 设置UI基础属性
function UIManager:SetupUI(ui, config)
    local parent = layerParents[config.layer]
    ui.transform:SetParent(parent,false)
    ui.transform:SetAsLastSibling()
    -- ui.transform.localPosition =Vector3.zero;
    -- ui.transform.localScale =Vector3.one;

end

-- 互斥检查处理
function UIManager:CheckMutex(newConfig)
    if newConfig.mutexGroup == MutexGroup.None then return end
    local panels = activePanels[newConfig.layer]
    if panels == nil  then return end
         
    for i = #panels, 1, -1 do
        self:CloseUI(panels[i])
    end
end


-- 弹性动画
function UIManager:PlayBounceAnimation(ui)
    -- ui.transform.localScale = Vector3.zero
    -- ui.transform:DOScale(Vector3.one,1)
    -- local seq = DOTween.Sequence()
    -- seq:Append(ui.transform:DOScale(Vector3(1.2, 1.2, 1.2), 0.2))
    -- seq:Append(ui.transform:DOScale(Vector3(0.9, 0.9, 0.9), 0.1))
    -- seq:Append(ui.transform:DOScale(Vector3.one, 0.1))
end

-- 关闭UI
function UIManager:CloseUI(uiName,isDestory)
    isDestory = isDestory or false
    if isDestory then
        local layer = UIConfig[uiName].layer
        local uiNames=  activePanels[layer]
        for k,v in ipairs(uiNames)  do
            if v == uiName then
                table.remove(activePanels,k)
                break
            end
        end
        openUIList[uiName].src:DeleteMe()
    else
        openUIList[uiName].src:OnDisable()
    end
end



return UIManager